<template>
  <div>
      <el-row>
          <el-col :span="24" align="right">
              <div style="margin-top:10px">
                <el-button @click="addNewRecord" type="primary">添加网格员</el-button>
                <el-input style="width:200px" v-model="filters.name" placeholder="请输入网格员姓名"></el-input>
                <el-input style="width:200px" v-model="filters.certID" placeholder="请输入网格员身份证"></el-input>
                <el-button type="primary" icon="search" @click="getInfo">搜索</el-button>
                <el-button type="primary" @click="flushAll">刷新</el-button>
              </div>
              <el-row style="margin-top:20px">
                  <el-col :span="$store.getters.user.userRole=='超级管理员'?3:0">
                      <treeComponent style="margin-top:-52px" @GetNodeValue="GetNodeValue"></treeComponent>
                  </el-col>
                  <el-col :span="$store.getters.user.userRole=='超级管理员'?21:24">
                      <el-table :data="list">
                          <el-table-column v-for="(v,k,i) in props" :key="i" :label="labels[i]" align="center">
                              <template scope="scope">
                                  <span v-if="k==='sex'">
                                       {{scope.row[k]==1?'男':'女'}}
                                  </span>
                                  <span v-else>
                                      {{scope.row[k]}}
                                  </span>
                              </template>
                          </el-table-column>
                          <el-table-column label="操作" align="center">
                              <template scope="scope">
                                  <el-button type="text" @click="ckDetail(scope.row.netGridMemberID)">查看详情</el-button>
                                  <el-button type="text" @click="editInfo(scope.row.netGridMemberID)">编辑</el-button>
                                  <el-button type="text" @click="deleteInfo(scope.row.netGridMemberID)">删除</el-button>
                              </template>
                          </el-table-column>
                      </el-table>
                  </el-col>
                  <el-col :span="24" style="margin-top:10px" align="right">
                      <el-pagination layout="prev, pager, next" :total="totalInfo" @current-change="handleCurrentChange">
                      </el-pagination>
                  </el-col>
              </el-row>
          </el-col>
      </el-row>
      <el-dialog title="网格员详细你信息" :visible.sync="visible">
        <div id="table">
         <table class="userInfoEditTable" style="border-collapse:collapse;background-color:white;" width="100%" cellspacing="0">
            <tr v-for="(v,k,i) in form" :key="v">
                <td class="tb_label">{{labels2[i]}}</td>
                <td>
                  <span v-if="k=='sex'">
                    {{v==1?'男':'女'}}
                  </span>
                  <span v-else>
                    {{v}}
                  </span>
                </td>
            </tr>
            <tr>
              <td colspan="2" align="center">
                <el-button type="text" size="large" @click="printTable('table')">[打印]</el-button>
                <el-button type="text" size="large" @click="$router.go(-1)">[返回]</el-button>
              </td>
          </tr>
        </table>
        </div>
      </el-dialog>
  </div>
</template>

<script>
import treeComponent from "../../components/common/treeComponent";
import * as api from "@/api/netGridManage";
export default {
  components: {
    treeComponent: treeComponent
  },
  data() {
    return {
      visible:false,
      filters: {
        name: null, //驾驶员姓名
        certID: null, //驾驶员身份证号
        netGridID: null, // 所属网格
        page: 1,
        pageSize: 10
      },
      list: [
        {
            netGridMemberID:'',//      bigint not null auto_increment,
            netGridID:'',//            bigint,
            name:'',//                varchar(50),
            sex:'',//                  int comment '值=1，代表男性；值=0，代表女性。',
            ethnicity:'',//            varchar(20) comment '民族',
            politics:'',//             varchar(20) comment '政治面貌',
            certID:'',//               varchar(18),
            birthDate:'',//            date,
            education:'',//            varchar(10) comment '学历',
            mobileTel:'',//            varchar(18) comment '移动电话',
            fixedTel:'',//             varchar(18) comment '固定电话',
        }
      ],
      totalInfo: 0,
      props: {
        name:'',//                varchar(50),
        sex:'',//                  int comment '值=1，代表男性；值=0，代表女性。',
        certID:'',//               varchar(18),
        mobileTel:'',//            varchar(18) comment '移动电话',
        netGridName:''
      },
      form:{

      },
      labels: ["姓名", "性别", "身份证", "移动电话","所属网格"],
      labels2: ["编号","所属网格","姓名", "性别", "民族","政治面貌","身份证","出生日期","学历","移动电话","固定电话"],
    };
  },
  methods: {
    printTable(div) {
      var div_print = document.getElementById(div);
      var newStr = div_print.innerHTML;
      var oldStr = document.body.innerHTML;
      document.body.innerHTML = newStr;
      window.print();
      window.location.reload();
      window.body.innerHTML = oldStr;
      return false;
    },
    deleteInfo(id) {
      this.$confirm("是否删除此驾驶员记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          api.deleteById(id).then(res => {
            if (res.data) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getInfo();
            } else {
              this.$message({
                type: "error",
                message: res.message
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    flushAll() {
      this.filters.name = this.filters.certID = this.filters.netGridID = null;
      this.getInfo();
    },
    handleCurrentChange(val) {
      this.filters.page = val;
      this.getInfo();
    },
    ckDetail(id) {
      this.visible = true
      api.getNetGridMemberDetail(id).then(res=>{
        this.form = res.data
      })
    },
    addNewRecord() {
      this.$router.push("add");
    },
    GetNodeValue(val) {
      this.filters.netGridID = val;
      this.getInfo();
    },
    editInfo(id) {
      this.$router.push({ path: "edit", query: { id: id } });
    },
    getInfo() {
      api.getInfoByPage(this.filters).then(res => {
        this.list = res.data.vos;
        this.totalInfo = res.data.p.totalCount;
      });
      this.filters.name = this.filters.certID = this.filters.netGridID = null;
    }
  },
  mounted() {
    this.getInfo();
  }
};
</script>


<style scoped>
.userInfoEditTable,
.userInfoEditTable td {
  border: 1px solid darkgrey;
  padding: 10px;
}

.tb_label {
  width: 20%;
  text-align: right;
}
</style>
